<?php
class LypoSignature {

	var $signature = "";

	public function __construct($sig=""){
		if($sig!=''){
			$this->setSignature($sig);
		}
	}

	public function getSignature(){
		return $this->signature;
	}

	public function getOppositeSignature(){
		$pairs = split(" ",$this->getSignature());
		$opposite_string = "";
		if(count($pairs)){
			$opposite_pairs = array();
			foreach($pairs as $key=>$answer){
				if(strpos($answer,StatementAnswerPeer::ANSWER_AGREE)>-1){
					$opposite_pairs[]=str_replace(StatementAnswerPeer::ANSWER_AGREE, StatementAnswerPeer::ANSWER_DISAGREE,$answer);
				}
				if(strpos($answer,StatementAnswerPeer::ANSWER_DISAGREE)>-1){
					$opposite_pairs[]=str_replace(StatementAnswerPeer::ANSWER_DISAGREE, StatementAnswerPeer::ANSWER_AGREE,$answer);
				}
			}
			$opposite_string = join(" ",$opposite_pairs);
		}
		return new LypoSignature($opposite_string);
	}
	
	public function getOppositePercentageFor(LypoSignature $other_sig){
		$self = $this->getValuePairs();
		$other = $other_sig->getValuePairs();
		//Alle mogelijke statement_ids mergen
		$keys = array_merge(array_keys($self),array_keys($other));
		$unique_keys = array_unique($keys);
		$num_answers = count($unique_keys);
		$diff_answers = 0;
		foreach($unique_keys as $key){
			if(isset($self[$key]) && isset($other[$key])){
				if($self[$key]!=StatementAnswerPeer::ANSWER_NOANSWER && $other[$key]!=StatementAnswerPeer::ANSWER_NOANSWER){
					if($self[$key]!=$other[$key]) $diff_answers++;
				}
			}
		}
		return ($diff_answers / $num_answers) * 100; 
	}
	

	public function getNumAnswers(){
		return count($this->_getPairs());
	}
	
	public function _getPairs(){
		return split(" ",$this->getSignature());
	}
	
	public function getValuePairs(){
		if(preg_match_all('/([0-9]{1,3})([a-c])/i',$this->getSignature(),$matches)){
			if(count($matches[1])==count($matches[2])){
				$data = array();
				foreach($matches[1] as $key => $temp_statement_id){
					$statement_id = intval($temp_statement_id);
					$data[$statement_id]=$matches[2][$key];
				} 
				return $data;
			}
		}
		return false;
	}
	
	
	public function setSignature($sig){
		$this->signature = $sig;
	}

}
?>
